package com.zhangjie.controller.api;

import com.zhangjie.common.exception.YyghException;
import com.zhangjie.common.helper.HttpRequestHelper;
import com.zhangjie.common.result.Result;
import com.zhangjie.common.result.ResultCodeEnum;
import com.zhangjie.common.util.MD5;
import com.zhangjie.model.hosp.Department;
import com.zhangjie.model.hosp.Hospital;
import com.zhangjie.model.hosp.Schedule;
import com.zhangjie.service.DepartmentService;
import com.zhangjie.service.HospitalService;
import com.zhangjie.service.HospitalSetService;
import com.zhangjie.service.ScheduleService;
import com.zhangjie.vo.hosp.DepartmentQueryVo;
import com.zhangjie.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    //查询医院接口
    @PostMapping("hospital/show")
    public Result getHospitalList(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();//返回一个map集合
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);   //把类型做一个转换
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1.拿到签名,是经过md5加密的
        String hospSign = (String) paramMap.get("sign");
        //2.根据医院传递过来的编号查询mg数据库，查询签名
        String signKey=hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询出来的数据也进行Md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            //如果不一样，抛异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法实现根据医院号查询
        Hospital hospital=hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //上传医院的接口
    @PostMapping("/saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();//返回一个map集合
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);   //把类型做一个转换
        //1.拿到签名,是经过md5加密的
        String hospSign = (String) paramMap.get("sign");
        //2.根据医院传递过来的编号查询mg数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey=hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询出来的数据也进行Md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            //如果不一样，抛异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中的 “+”转化为“ ”
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData",logoData);
        //调用service处理
        hospitalService.save(paramMap);
        return Result.ok();
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的科室信息
        Map<String, String[]> requestMap = request.getParameterMap();//返回一个map集合
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);   //把类型做一个转换
        //1.拿到签名,是经过md5加密的
        String hospSign = (String) paramMap.get("sign");
        //2.根据医院传递过来的编号查询mg数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey=hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询出来的数据也进行Md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            //如果不一样，抛异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法  他里面有操作mongoDb的 DepartmentRepository
        departmentService.save(paramMap);
        return Result.ok();
    }
    //查询科室接口
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        //拿到传递过来的
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        checkSignature(paramMap);//调用方法拿到map里面的编号查询数据库，解析完和传入的签名比较   来完成校验
        String  hoscode = (String) paramMap.get("hoscode");//医院编号
        //下面去拿分页信息
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service来进行分页查询
        Page<Department> pageModel=departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }

    //删除科室接口
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        checkSignature(paramMap);//调用方法拿到map里面的编号查询数据库，解析完和传入的签名比较   来完成校验
        //医院编号 和 科室编号
        String  hoscode = (String) paramMap.get("hoscode");
        String  depcode = (String) paramMap.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //上传排版接口
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        checkSignature(paramMap);//调用方法拿到map里面的编号查询数据库，
        scheduleService.save(paramMap);
        return Result.ok();
    }
    //查询排班接口
    @PostMapping("/schedule/list")
    public Result findSchedule(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        checkSignature(paramMap);//调用方法拿到map里面的编号查询数据库，解析完和传入的签名比较   来完成校验
        String  hoscode = (String) paramMap.get("hoscode");//医院编号
        String depcode = (String) paramMap.get("depcode");//科室编号
        //下面去拿分页信息
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        ScheduleQueryVo queryVo = new ScheduleQueryVo();   //封装一个查询的类  一般是拖敏的
        queryVo.setHoscode(hoscode);
        queryVo.setDepcode(depcode);
        //调用service来进行分页查询
        Page<Schedule> pageModel=scheduleService.findPageSchedule(page,limit,queryVo);
        return Result.ok(pageModel);
    }

    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        checkSignature(paramMap);//调用方法拿到map里面的编号查询数据库，解析完和传入的签名比较   来完成校验
        //获取医院编号和排版编号
        String  hoscode = (String) paramMap.get("hoscode");//医院编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        checkSignature(paramMap);//调用方法拿到map里面的编号查询数据库，解析完和传入的签名比较   来完成校验
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();

    }

    //私有校验签名功能     待测试
    private void checkSignature(Map<String, Object> paramMap ){
        //1.拿到签名,是经过md5加密的
        String hospSign = (String) paramMap.get("sign");
        //2.根据医院传递过来的编号查询mg数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey=hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询出来的数据也进行Md5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(signKeyMD5)){
            //如果不一样，抛异常
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }
}
